#------------------------------------------------------------------------------#
########################            Maps           #############################
#------------------------------------------------------------------------------#

#Loading packages to work with maps
library(tidyverse)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(pollster)

# Setting directory: adjust accordingly to 

setwd()

#------------------------------------------------------------------------------#
### Importing Base  ############################################################
#------------------------------------------------------------------------------#

Base <- readRDS("data/Base.rds")

#------------------------------------------------------------------------------#
### Renaming Variables  ########################################################
#------------------------------------------------------------------------------#

Base <- Base %>%
  mutate(
    bf = `Belief climate change`,
    cau = `Causes of climate change`,
    imp = `Impacts of climate change`,
    pi_lf = `Political Ideology (Left-Right)`,
    pi_cs = `Political Ideology (Conservatism-Progressive)`, 
    sk = `Subjective knowledge`,
    ok = `Objective knowledge`,
    sc = `Scientific consensus`,
    ts = `Trust in scientists`, 
    nep = `The New Ecological Paradigm (NEP)`,
    ii = `Individualism index`,
    ei = `Egalitarianism index`,
    pe = `Personal Experience (extreme weather events)`)

#------------------------------------------------------------------------------#
### Importing data of borders in the Americas ##################################
#------------------------------------------------------------------------------#

america <- ne_countries(scale = "medium", type = "countries", continent = c("south america", "north america"), returnclass = "sf")

# Plotting plain map, with zoom (i.e. coord_sf) in the latin american region
ggplot(data = america) +
  geom_sf() +
  coord_sf(xlim = c(-120,-33), ylim = c(-60,35), expand = FALSE) +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), #without crossed lines
        panel.grid.minor = element_blank())

#------------------------------------------------------------------------------#
### Existence ##############################################################
#------------------------------------------------------------------------------#

# Creating cross table of percentage of respondents in climate existence (0-8) https://cran.r-project.org/web/packages/pollster/readme/README.html
belief <- crosstab(df = Base, x = Pais, y = bf, weight = ponde2) %>%
  mutate(name = case_when(
    Pais == 1 ~ "Brazil",
    Pais == 2 ~ "Argentina",
    Pais == 3 ~ "Chile",
    Pais == 4 ~ "Colombia",
    Pais == 5 ~ "Ecuador",
    Pais == 6 ~ "Mexico",
    Pais == 7 ~ "Peru",
  )) %>%
  rename(belief_5 = '5',
         belief_6 = '6',
         belief_7 = '7',
         belief_8 = '8') %>%
  mutate(belief = belief_5 + belief_6 + belief_7 + belief_8) %>%
  dplyr::select(name, belief) 

# Joining existence data with america df
america <- america %>% full_join(belief, by = "name")

# Plotting map filled with % of respondents which believe in climate change existence
ggplot(data = america) +
  geom_sf(aes(fill = belief), color = "#808080") +
  coord_sf(xlim = c(-120,-33), ylim = c(-60,35), expand = FALSE) +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.title = element_text(size = 8.5),
        legend.spacing.y = unit(0.4, 'cm')) +
  scale_fill_binned(name = "% of respondents who \n believe in climate change \n existence",
                    type = "viridis",
                    breaks = c(55, 65, 75, 85, 90, 95),
                    limits = c(55, 100),
                    guide = guide_coloursteps(even.steps = TRUE,
                                              show.limits = TRUE),
                    alpha = 0.8)

#------------------------------------------------------------------------------#
### Causes of Climate Change ###################################################
#------------------------------------------------------------------------------#

# Creating cross table of percentage of respondents who believe climate change is caused by human activity
causes <- crosstab(df = Base, x = Pais, y = cau, weight = ponde2) %>%
  mutate(name = case_when(
    Pais == 1 ~ "Brazil",
    Pais == 2 ~ "Argentina",
    Pais == 3 ~ "Chile",
    Pais == 4 ~ "Colombia",
    Pais == 5 ~ "Ecuador",
    Pais == 6 ~ "Mexico",
    Pais == 7 ~ "Peru",
  )) %>%
  rename(causes = '1') %>%
  dplyr::select(name, causes)

# Joining belief data with america df
america <- america %>% full_join(causes, by = "name")

# Plotting map filled with % of respondents which believe climate change is caused primarily by human activity
ggplot(data = america) +
  geom_sf(aes(fill = causes), color = "#808080") +
  coord_sf(xlim = c(-120,-33), ylim = c(-60,35), expand = FALSE) +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.title = element_text(size = 8.5),
        legend.spacing.y = unit(0.4, 'cm')) +
  scale_fill_binned(name = "% of respondents who \n believe climate change \n is caused primarily by \n human activity",
                    type = "viridis",
                    breaks = c(55, 65, 75, 85, 90, 95),
                    limits = c(55, 100),
                    guide = guide_coloursteps(even.steps = TRUE,
                                              show.limits = TRUE),
                    alpha = 0.8)

#------------------------------------------------------------------------------#
### Consequences of Climate Change ##################################################
#------------------------------------------------------------------------------#

# Creating cross table of percentage of respondents who believe climate change will have negative effects
impacts <- crosstab(df = Base, x = Pais, y = imp, weight = ponde2) %>%
  mutate(name = case_when(
    Pais == 1 ~ "Brazil",
    Pais == 2 ~ "Argentina",
    Pais == 3 ~ "Chile",
    Pais == 4 ~ "Colombia",
    Pais == 5 ~ "Ecuador",
    Pais == 6 ~ "Mexico",
    Pais == 7 ~ "Peru",
  )) %>%
  rename(impacts = '1') %>%
  dplyr::select(name, impacts) 

# Joining belief data with america df
america <- america %>% full_join(impacts, by = "name")

# Plotting map filled with % of respondents which believe climate change is caused primarily by human activity
ggplot(data = america) +
  geom_sf(aes(fill = impacts), color = "#808080") + 
  coord_sf(xlim = c(-120,-33), ylim = c(-60,35), expand = FALSE) +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.title = element_text(size = 8.5),
        legend.spacing.y = unit(0.4, 'cm')) +
  scale_fill_binned(name = "% of respondents who \n believe climate change \n will have a negative impact",
                    type = "viridis",
                    breaks = c(55, 65, 75, 85, 90, 95),
                    limits = c(55, 100),
                    guide = guide_coloursteps(even.steps = TRUE,
                                              show.limits = TRUE),
                    alpha = 0.8)


#------------------------------------------------------------------------------#
### Belief in Climate Change Index (Map) #######################################
#------------------------------------------------------------------------------#

# Creating cross table of percentage of respondents who have scores above 0
index <- Base %>%
  group_by(Pais) %>%
  summarise(mean_score = weighted.mean(index_score, w=ponde2, na.rm = T)) %>%
  mutate(name = case_when(
    Pais == 1 ~ "Brazil",
    Pais == 2 ~ "Argentina",
    Pais == 3 ~ "Chile",
    Pais == 4 ~ "Colombia",
    Pais == 5 ~ "Ecuador",
    Pais == 6 ~ "Mexico",
    Pais == 7 ~ "Peru")) %>%
  dplyr::select(name, mean_score)
  
# Joining belief data with america df
america <- america %>% full_join(index, by = "name")

# Plotting map filled with % of respondents which believe climate change is caused primarily by human activity
ggplot(data = america) +
  geom_sf(aes(fill = mean_score), color = "#808080") + 
  coord_sf(xlim = c(-120,-33), ylim = c(-60,35), expand = FALSE) +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        legend.title = element_text(size = 8.5),
        legend.spacing.y = unit(0.4, 'cm')) +
  scale_fill_binned(name = "Average factor scores",
                    type = "viridis",
                    breaks = c(-0.10, -0.05, 0, 0.05, 0.10, 0.15),
                    limits = c(-0.10, 0.15),
                    guide = guide_coloursteps(even.steps = TRUE,
                                              show.limits = TRUE),
                    alpha = 0.8)
